package com.atguigu.gmall.product.config;

import io.minio.MinioClient;
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @author lsj
 * @create 2021-08-05 0:42
 *
 *      单独的Minio的配置类
 */
@ConfigurationProperties(prefix = "minio")
@Configuration
@Data
public class MinioConfig {

    private String url;
    private String accessKey;
    private String secretKey;
    private String defaultBucket;

    public MinioConfig(){
        System.out.println("进入spring容器。。。。");
    }

    @Bean
    public MinioClient minioClient(){
        try {
            MinioClient minioClient = new MinioClient(
                    url,
                    accessKey,
                    secretKey);
            boolean exists = minioClient.bucketExists(defaultBucket);
            if(!exists){
                minioClient.makeBucket(defaultBucket);
            }
            return minioClient;
        } catch (Exception e) {
            return null;
        }
    }

}

